<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 品牌管理
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: brand.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 */
class Brand extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Brand_model');
            $list = $this->Brand_model->browse_brand($params, $params['page'], $params['size']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 筛选
        $pagesize     = $this->config->item('pagesize');

        // 统计
        $this->load->dao('Brand_dao');
        $total = $this->Brand_dao->count(array(
            'is_delete' => UNDELETED
        ));

        $data = array(
            'default'      => $params ? $params : array(),
            'total'        => number_format($total),
            'list'         => $list,
            'brand_status' => $this->config->item('brand_status'),
            'pagination'   => $pagination,
            'pagesize'     => $pagesize,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('brand/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [查看]
     */
    public function view($sn = NULL)
    {
        $this->load->model('Brand_model');
        if( ! $brand = $this->Brand_model->get_brand($sn, FALSE))
        {
            show_error($this->Brand_model->get_error(), 200, '错误', 'error_general_lite');
        }

        // 绑定操作人数据
        $this->load->model('Rbac_model');
        $this->Rbac_model->wrap_admin_info($brand, TRUE, 'create_admin');
        $this->Rbac_model->wrap_admin_info($brand, TRUE, 'update_admin');

        $page_style = $this->config->item('page_style');
        $data = array(
            'brand'         => $brand,
            'brand_status'  => $this->config->item('brand_status'),
            'page_style'    => $page_style['LITE'],
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('brand/view.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_brand', $params);

            $this->load->model('Brand_model');

            if( FALSE === $brand_sn = $this->Brand_model->add_brand($params) )
            {
                show_error($this->Brand_model->get_error(), 200, '错误', 'error_general_lite');
            }
            temp('add_brand', NULL);

            show_message(
                "新增品牌成功！", 
                array('查看新建的品牌' => DOMAIN."brand/view/{$brand_sn}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'         => temp('add_brand'),
                'page_style'   => $page_style['LITE'],
                'brand_status' => $this->config->item('brand_status'), 
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('brand/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑]
     */
    public function edit($sn = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Brand_model');

            if( FALSE === $this->Brand_model->edit_brand($sn, $params) )
            {
                show_error($this->Brand_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑品牌成功！", 
                array('查看编辑的品牌' => DOMAIN."member/view/{$sn}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Brand_model');
            if( ! $brand = $this->Brand_model->get_brand($sn, FALSE))
            {
                show_error($this->Brand_model->get_error(), 200, '错误', 'error_general_lite');
            }

            $page_style    = $this->config->item('page_style');
            $data = array(
                'brand'        => $brand,
                'page_style'   => $page_style['LITE'],
                'brand_status' => $this->config->item('brand_status'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('brand/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }


    /**
     * [改变状态]
     */
    public function change_status($sn = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Brand_model');
        if( FALSE === $this->Brand_model->change_brand_status($sn, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Brand_model->get_error());
            }
            else
            {
                show_error($this->Brand_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变品牌状态成功！", array(
                    '返回会员管理' => DOMAIN."brand/browse",
                ));
            }
        }
    }

}
/* End of file */